const app = getApp();
import {
  discriCard
} from "../../utils/util.js";
import axios from "../../http/axios.js";
import {
  apiGetServiesNum,
  apiGetrecentService
} from "../../http/server.js";
import {
  commonGetNotice,
  commonToNoticeDetail
} from "../../assets/js/notice.js";
import {
  formatTime
} from "../../utils/util.js"

Page({
  data: {
    noList: {
      img: "/assets/img/zwjl.png",
      text: "暂无记录"
    },
    // 服务状态数据
    serviceStates: [{
        num: 0,
        type: "未开始"
      },
      {
        num: 2,
        type: "进行中"
      },
      {
        num: 10,
        type: "已完成"
      },
      {
        num: 0,
        type: "已取消"
      }
    ],
    // 近期服务
    recentServicesList: [{
        icon: "/assets/img/pthy.png",
        nav: "普通换药"
      },
      {
        icon: "/assets/img/fwdz.png",
        nav: "浙江省杭州市滨江区7栋"
      },
      {
        icon: "/assets/img/fwsj.png",
        nav: "2019/03/32 13:00~15:00"
      }
    ],
    // 健康公告
    notice: [{
        item: [{
            title: "公告1"
          },
          {
            title: "公告2"
          }
        ]
      },
      {
        item: [{
            title: "公告3"
          },
          {
            title: "公告4"
          }
        ]
      }
    ],
    // 用户信息
    n_userInfo: {
      name: "未知",
      age: "未知",
      sex: "未知",
      image: "/assets/img/left.png"
    },
    // 安全保障
    content: {
      title: "安全保障",
      list: [{
          image: "/assets/img/yjbj.png",
          text: "一键报警"
        },
        {
          image: "/assets/img/xcfx.png",
          text: "行程分享"
        },
        {
          image: "/assets/img/zblx.png",
          text: "直播录像"
        },
        {
          image: "/assets/img/jjlxr.png",
          text: "紧急联系人",
          path: "/pages/n_urgentContacts/n_urgentContacts"
        },
      ]
    },
    // 个人中心
    content1: {
      title: "个人中心",
      list: [
        // {
        //   image: "/assets/img/hzpj.png",
        //   text: "患者评价",
        //   path: "/pages/n_urgentContacts/n_urgentContacts"
        // },
        {
          image: "/assets/img/zzkf.png",
          text: "正在开发",
          path: "/pages/n_urgentContacts/n_urgentContacts"
        }
      ]
    }
  },
  onLoad: function(options) {},
  onReady: function() {},
  onShow: function() {
    this.setUserInfo();
    this.getNotice();
    this.getServiesNum();
    this.getrecentService();
  },
  // 获取近期服务
  getrecentService() {
    axios({
      url: apiGetrecentService,
      data: {
        workersId: wx.getStorageSync("userInfo").workersId
      }
    }).then(res => {
      // console.log("近期服务", res);
      let recentServicesList = [];
      if (res) {
        recentServicesList = [{
            icon: "/assets/img/pthy.png",
            nav: res.serviceName
          },
          {
            icon: "/assets/img/fwdz.png",
            nav: res.serviceAddress
          },
          {
            icon: "/assets/img/fwsj.png",
            nav: formatTime(new Date(res.serviceTime)).replace(/\//g, "-")
          }
        ]
      } else {
        recentServicesList = [];
      }
      this.setData({
        recentServicesList
      })
    })
  },
  // 获取订单条数
  getServiesNum() {
    axios({
      url: apiGetServiesNum,
      data: {
        workersId: wx.getStorageSync("userInfo").workersId
      }
    }).then(res => {
      // console.log("服务状态数量显示", res)
      let serviceStates = [{
          num: 0,
          type: "待服务"
        },
        {
          num: 0,
          type: "进行中"
        },
        {
          num: 0,
          type: "已完成"
        },
        {
          num: 0,
          type: "已取消"
        }
      ]
      res.forEach(item => {
        switch (Number(item.status)) {
          case 3: // 待服务
            {
              serviceStates[0].num += item.sum;
              break;
            }
          case 4: // 进行中
            {
              serviceStates[1].num += item.sum;
              break;
            }
          case 5: // 已完成
            {
              serviceStates[2].num += item.sum;
              break;
            }
          case 7: // 已取消
            {
              serviceStates[3].num += item.sum;
              break;
            }
          case 8: // 已取消
            {
              serviceStates[3].num += item.sum;
              break;
            }
          default:
            break;
        }
      })
      this.setData({
        serviceStates
      })
    })
  },
  // 跳转到职工服务区域
  handleClickToServices: function() {
    wx.navigateTo({
      url: '/pages/n_services/n_services',
    })
  },
  // 跳转到公告详情
  toNoticeDetail: function(e) {
    let item = e.detail;
    commonToNoticeDetail(item);
  },
  // 获取10条公告
  getNotice: function() {
    commonGetNotice(this);
  },
  // 安全保障中跳转其他页面
  toOtherPage: function(e) {
    let item = e.detail;
    wx.navigateTo({
      url: `${item.path}`
    })
  },
  // 个人信息页
  handleClickToUserInfo: function(e) {
    wx.navigateTo({
      url: `/pages/n_personalInformation/n_personalInformation?userType=护士`,
    })
  },
  // 设置用户信息
  setUserInfo: function() {
    let userInfo = wx.getStorageSync("userInfo");
    this.setData({
      n_userInfo: {
        name: userInfo.userName ? userInfo.userName : "未知",
        age: userInfo.idCard ? discriCard(String(userInfo.idCard)).age : "未知",
        sex: userInfo.idCard ? discriCard(String(userInfo.idCard)).sex : "未知",
        image: "/assets/img/left.png"
      }
    })
  }
})